package com.backsidecode.controller;

import com.backsidecode.common.BaseContext;
import com.backsidecode.common.Result;
import com.backsidecode.pojo.entity.Courses;
import com.backsidecode.pojo.entity.PageBean;
import com.backsidecode.pojo.entity.User;
import com.backsidecode.service.CoursesService;
import com.backsidecode.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(tags = "课程信息管理")
@CrossOrigin
@RequestMapping("/Course")
public class CourseController {
    @Autowired
    CoursesService coursesService;

    @Autowired
    UserService userService;

    @ApiOperation(value = "新增课程信息")
    @PostMapping("/insertCourse")
    public Result<String> insertCourse(@RequestBody Courses course) {
        //获取当前登陆人
        Long currentId = BaseContext.getCurrentId();
        log.info("新增课程信息:{}", course);

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getId, currentId);
        User user = userService.getOne(lambdaQueryWrapper);
        if (user.getStatus().equals("1")) {
            coursesService.save(course);
            return Result.success("新增课程信息成功");
        } else {
            return Result.error("无权限新增课程");
        }
    }


    @ApiOperation(value = "查询课程信息（分页）")
    @GetMapping("/selectCoursePage")
    public Result<PageBean> selectCoursePage(@RequestParam(defaultValue = "1") int page,
                                             @RequestParam(defaultValue = "10") int pageSize,
                                             String className) {

        log.info("page = {} , pageSize = {}, name = {}", page, pageSize, className);

        PageBean pageBean = coursesService.getCoursePage(page, pageSize, className);

        return Result.success(pageBean);
    }


    @ApiOperation(value = "查询课程信息")
    @GetMapping("/selectCourse")
    public Result<List<Courses>> selectCourse() {

        //查询所有课程信息
        List<Courses> list = coursesService.list();

        return Result.success(list);
    }


    @ApiOperation(value = "删除课程信息")
    @PostMapping("/deleteCourse")
    public Result<String> deleteCourse(@RequestBody Courses course) {
        //获取当前登陆人
        Long currentId = BaseContext.getCurrentId();

        log.info("删除课程信息:{}，当前操作人:{}", course.getId(), currentId);

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getId, currentId);
        User user = userService.getOne(lambdaQueryWrapper);
        if (user.getStatus().equals("1")) {
            coursesService.deleteCourse(course.getId());
            return Result.success("删除课程信息成功");
        } else {
            return Result.error("无权限删除课程信息");
        }
    }


}
